package com.dys.hejiayuncommunity.web.controller.community;

import com.dys.hejiayuncommunity.common.constant.HttpStatus;
import com.dys.hejiayuncommunity.common.core.controller.BaseController;
import com.dys.hejiayuncommunity.common.core.domain.BaseResponse;
import com.dys.hejiayuncommunity.common.core.page.PageResult;
import com.dys.hejiayuncommunity.common.utils.ServletUtils;
import com.dys.hejiayuncommunity.community.domain.HjyCommunity;
import com.dys.hejiayuncommunity.community.domain.dto.HjyCommunityDTO;
import com.dys.hejiayuncommunity.community.domain.vo.HjyCommunityVO;
import com.dys.hejiayuncommunity.community.service.HjyCommunityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @className: HjyCommunityController
 * @description: 小区控制器类
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/12/03 23:32
 */
@RestController
@RequestMapping(value = "/community")
public class HjyCommunityController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(HjyCommunityController.class);
    @Autowired
    private HjyCommunityService hjyCommunityService;

    // http://localhost:8888/community/list?pageNum=1&pageSize=10
    @GetMapping(value = "/list")
    public PageResult list(HjyCommunity hjyCommunity) {
        // 获取分页参数
        Integer pageNum = ServletUtils.getParameterToInt("pageNum");
        Integer pageSize = ServletUtils.getParameterToInt("pageSize");

        // 使用PageHelper
        PageHelper.startPage(pageNum, pageSize);
        List<HjyCommunityDTO> list = hjyCommunityService.findCommunityByCondition(hjyCommunity);

        // 封装数据
        PageResult pageResult = new PageResult();
        pageResult.setCode(HttpStatus.SUCCESS);
        pageResult.setMessage("分页查询小区数据成功");
        pageResult.setTotal(new PageInfo<>(list).getTotal());
        pageResult.setRows(list);

        return pageResult;
    }

    /**
     * 增加权限校验
     */
    @PreAuthorize("@pe.hasPerms('system:community:list')")
    @GetMapping(value = "/listCommunity")
    public PageResult listCommunity(HjyCommunity hjyCommunity) {
        // 开始分页,设置分页参数
        startPage();
        List<HjyCommunityDTO> list = hjyCommunityService.findCommunityByCondition(hjyCommunity);
        return getData(list);
    }

    @PostMapping(value = "/addCommunity")
    public BaseResponse addCommunity(@RequestBody HjyCommunity hjyCommunity) {
        return toAjax(hjyCommunityService.addCommunity(hjyCommunity));
    }

    @GetMapping(value = "/getCommunityInfo/{communityId}")
    public BaseResponse<HjyCommunity> getCommunityInfoById(@PathVariable(value = "communityId") Long communityId) {
        HjyCommunity communityInfo = hjyCommunityService.findCommunityInfoById(communityId);
        return BaseResponse.success(communityInfo);
    }

    @PutMapping(value = "/updateCommunity")
    public BaseResponse updateCommunity(@RequestBody HjyCommunity hjyCommunity) {
        return toAjax(hjyCommunityService.updateCommunityById(hjyCommunity));
    }

    @DeleteMapping(value = "/deleteCommunityByIds/{communityIds}")
    public BaseResponse deleteCommunityByIds(@PathVariable(value = "communityIds") Long[] communityIds) {
        return toAjax(hjyCommunityService.deleteBatchCommunityByIds(communityIds));
    }

    @GetMapping(value = "/dropDownSelectedArea")
    public BaseResponse<List<HjyCommunityVO>> findAreaList(HjyCommunity hjyCommunity){
        // 打印入参日志
        log.info("入参数据=>[HjyCommunity={}]", hjyCommunity);
        List<HjyCommunityVO> voList = null;
        try {
            voList = hjyCommunityService.dropDownSelectedArea(hjyCommunity);
        }catch (Exception e){
            log.warn("获取小区下拉列表失败!",e);
        }
        // 打印返回结果
        log.info("返回结果：{}",voList);
        return BaseResponse.success(voList);
    }
}
